/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormListMaquinas.java
 *
 * Created on 01/09/2011, 17:54:23
 */
package javalee.forms;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javalee.util.Uteis;
import javalee.maps.Maquina;
import javalee.dao.maquinaDAO;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Thiago
 */
public class FormListMaquinas extends javax.swing.JFrame {
    
     private maquinaDAO maqDAO;
     private JTextField j;

    /** Creates new form FormListMaquinas */
    public FormListMaquinas(JTextField jparametro) {
        initComponents();
        maqDAO = new maquinaDAO();
        j = jparametro;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbConsulta = new javax.swing.JTable();
        btnOK = new javax.swing.JButton();
        txtConsulta = new javax.swing.JTextField();

        setTitle("Formulario de Lista de Maquinas");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        tbConsulta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Máquina"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbConsulta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbConsultaMouseReleased(evt);
            }
        });
        tbConsulta.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                tbConsultaComponentShown(evt);
            }
        });
        jScrollPane2.setViewportView(tbConsulta);

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        txtConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtConsultaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(126, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void tbConsultaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbConsultaMouseReleased
    int row = tbConsulta.getSelectedRow();
    
    DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();  
   
}//GEN-LAST:event_tbConsultaMouseReleased

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
 try {
      //  formWindowClosed(null);
        int row = tbConsulta.getSelectedRow();
    
    DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();  
   // Maquina maqEx = new Maquina(Integer.parseInt());
    j.setText(tableModel.getValueAt(row, 0).toString());
    } 
 catch (Exception e) {
    }
 dispose();
}//GEN-LAST:event_btnOKActionPerformed

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
preencheDados();
}//GEN-LAST:event_formWindowActivated

private void txtConsultaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtConsultaKeyReleased
    if(evt.getKeyCode()==10){
        preencheDados();
    }
}//GEN-LAST:event_txtConsultaKeyReleased

private void tbConsultaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_tbConsultaComponentShown
preencheDados();
    // TODO add your handling code here:
}//GEN-LAST:event_tbConsultaComponentShown

 
private void preencheDados(){
    if(!Uteis.validaCampoTexto(txtConsulta)){
            JOptionPane.showMessageDialog(null,"Campo de pesquisa está incorreto.");
        }
        else{
     //Trago a lista de maquinas do banco
     ArrayList<Maquina> lista = maqDAO.consultar(txtConsulta.getText().toString());
     tbConsulta.removeAll();
     
     //Pego o TableModel do componente JTable
     DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();  
     //Limpo os dados do componente
     tableModel.setNumRows(0);
     
     //Passo pela lista de Maquinas, inserindo no JTable
     for(Iterator<Maquina> it = lista.iterator(); it.hasNext();){
        Maquina c = (Maquina)it.next();
        tableModel.addRow(new Object[]{
            c.getId(),//Cada campo que coloco vira uma coluna
            c.getDescricao()
        });     
    }   
}
}


    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tbConsulta;
    private javax.swing.JTextField txtConsulta;
    // End of variables declaration//GEN-END:variables

    private void formWindowClosed(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
